﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using SpeedikonCom;

namespace SOM.Objects
{
  [Serializable()]
  [ComDefaultInterface(typeof(SOM.Interfaces.Objects.IOpening)), ClassInterface(ClassInterfaceType.None)]
  [ComSourceInterfaces(typeof(SOM.Interfaces.Objects.IOpening))]
  public partial class Opening : SOM.Interfaces.Objects.IOpening
  {

    #region "COM-GUIDs"
    // Diese GUIDs stellen die COM-Identität für diese Klasse 
    // und ihre COM-Schnittstellen bereit. Wenn Sie sie ändern, können vorhandene 
    // Clients nicht mehr auf die Klasse zugreifen.
    public const string ClassId = "27FC84E4-B742-4207-9B8D-D096B9BF3941";
    public const string InterfaceId = "CA1CE7B9-1A1A-4C52-AB8D-4127451250AD";
    public const string EventsId = "9A4CC4BC-E669-417B-AC33-59F5B8A354F7";
    #endregion

    [ComVisible(false)]
    const string sOeffnungName = "Oeffnung";
    [ComVisible(false)]
    const string sOeffnungZellName = "Opening";

    [ComVisible(false)]
    private Dictionary<string, int> ldObjektTyp = new Dictionary<string, int>();
    [ComVisible(false)]
    private List<string> scObjektName = new List<string>();
    [ComVisible(false)]
    private Dictionary<string, string> sdObjektZellName = new Dictionary<string, string>();
    [ComVisible(false)]
    private Dictionary<string, Dictionary<string, string>> sddObjektAttribut = new Dictionary<string, Dictionary<string, string>>();

   
    public Opening()
    {
      _StartPoint = new SOM.Helper.Types.Point3d { X = 0, Y = 0, Z = 0 };
      _props = this.GetType().GetProperties();
 
    }

    public bool Delete()
    {
      IEZ_sp_Object o = new IEZ_sp_Object();
      int iRet;
      o.Delete((int)SOM.Helper.enums.ObjektTypen.Opening, this.ID, out iRet);
      if (iRet != 0)
        return false;
      else
        return true;
    }

    public object GetCustomAttribute(string AttributeName, SOM.Helper.enums.ReturnType ReturnType, out int iRet)
    {
        int lStatus = 0;
        int Anz;
        double dVal = 0;
        double[] daVal = null;
        int lVal = 0;
        int[] laVal = null;
        string sVal = null;
        string[] saVal = null;
        object ret = null;

        int Typ = (int)SOM.Helper.enums.ObjektTypen.Opening;
        IEZ_sp_Attribute cAttribut = new IEZ_sp_Attribute();
        switch (ReturnType)
        {
            case SOM.Helper.enums.ReturnType.Double:
                dVal = 0;
                cAttribut.GetDouble((int)Typ, ID, AttributeName, out dVal, out lStatus);
                if (lStatus == 0)
                {
                    ret = dVal;
                }

                break;
            case SOM.Helper.enums.ReturnType.DoubleArray:
                daVal = null;
                Anz = 0;
                cAttribut.GetArraySize(Typ, ID, AttributeName, out Anz, out lStatus);
                cAttribut.GetDoubleArray(Typ, ID, AttributeName, 0, Anz, out daVal, out lStatus);
                if (lStatus == 0)
                {
                    ret = daVal;
                }
                break;
            case SOM.Helper.enums.ReturnType.Integer:
                lVal = 0;
                cAttribut.GetInt(Typ, ID, AttributeName, out lVal, out lStatus);
                if (lStatus == 0)
                {
                    ret = lVal;
                }
                break;
            case SOM.Helper.enums.ReturnType.IntegerArray:
                laVal = null;
                Anz = 0;
                cAttribut.GetArraySize(Typ, ID, AttributeName, out Anz, out lStatus);
                cAttribut.GetIntArray(Typ, ID, AttributeName, 0, Anz, out laVal, out lStatus);
                if (lStatus == 0)
                {
                    ret = laVal;
                }
                break;
            case SOM.Helper.enums.ReturnType.String:
                sVal = null;
                cAttribut.GetChar(Typ, ID, AttributeName, out sVal, out lStatus);
                if (lStatus == 0)
                {
                    ret = sVal;
                }
                break;
            case SOM.Helper.enums.ReturnType.StringArray:
                saVal = null;
                Anz = 0;
                cAttribut.GetArraySize(Typ, ID, AttributeName, out Anz, out lStatus);
                cAttribut.GetCharArray(Typ, ID, AttributeName, 0, Anz, out saVal, out lStatus);
                if (lStatus == 0)
                {
                    ret = saVal;
                }
                break;
            default:
                lStatus = -9999;
                break;
        }
        iRet = lStatus;
        return ret;
    }


    public bool Place(ref SOM.Helper.Types.Point3d Origin)
    {
      throw new NotImplementedException("COM-Interface doesn't support placing Openings");
      //IEZ_sp_Opening w = new IEZ_sp_Opening();
      //int iRet;
      //w.InitPlace(1,(int)this.SubType, this.A_PROTOTYPE, out iRet);
      //if (iRet != 0)
      //  return false;
      //else
      //{
      //  w.Place(this.StartPointAxis.X, this.StartPointAxis.Y, this.EndPointAxis.X, this.EndPointAxis.Y, out iRet);
      //  if (iRet != 0)
      //    return false;
      //  else
      //    return true;
      //}
    }

    public void SendToDatabase(string server, string table, string SchemeFilePath = "")
    {
      SOM.Helper.Methods.SendToDatabase<SOM.Objects.Opening>(this, server,table,SchemeFilePath);
    }
    public void GetFromDatabase(string server,int ID, string table,string SchemeFilePath = "")
    {
      var tmp = (SOM.Interfaces.Objects.IOpening)SOM.Helper.Methods.GetFromDatabase<SOM.Objects.Opening>(server, ID,table,SchemeFilePath);
      foreach (System.Reflection.PropertyInfo p in this.GetType().GetProperties())
      {
        try { this.GetType().GetProperty(p.Name).SetValue(this, tmp.GetType().GetProperty(p.Name).GetValue(tmp, null), null); }
        catch { }
      }
    }

    private System.Reflection.PropertyInfo[] _props;

    

    bool _readOnly = false;
    internal void SetPropertiesReadOnly(bool ReadOnly)
    {
      _readOnly = ReadOnly;
    }

   

    public void LoadOpening(int OpeningID)
    {


      List<string> sdObjektName = new List<string>();
      Dictionary<string, string> sddOjektAttribut = new Dictionary<string, string>();

      Dictionary<string, string> sdAttribute = new Dictionary<string, string>();

      IEZ_sp_Opening clOpening = new IEZ_sp_Opening();
      IEZ_sp_Object clObject = new IEZ_sp_Object();


      int counter;
      counter = 0;

      int iCntWindows;

      int[] iIdWindows;

      int iRet;

      SOM.Helper.Types.Point3d point1;
      SOM.Helper.Types.Point3d point2;

      iIdWindows = null;

      clObject.GetList((int)5, out iCntWindows, out  iIdWindows, out iRet);



      point1.X = 0;
      point1.Y = 0;
      point1.Z = 0;
      point2.X = 0;
      point2.Y = 0;
      point2.Z = 0;




      int _SubType;
      int _WallID;
      double startX;
      double startY;
      double parapetheight;
      double angle;
      double width;
      double height;
      double depth;
      clOpening.ParamGet(OpeningID, out _SubType, out _WallID, out startX, out startY, out parapetheight, out angle, out width, out height, out depth, out iRet);
      counter = counter + 1;

      this.ID = OpeningID;
      this.WallID = _WallID;
      this.SubType = (SOM.Helper.enums.subType)_SubType;

      this.StartPoint = new SOM.Helper.Types.Point3d { X = Math.Round(startX, 2, MidpointRounding.AwayFromZero), Y = Math.Round(startY, 2, MidpointRounding.AwayFromZero), Z = 0 };

      this.ParapetHeight = parapetheight;
      this.Angle = angle;//Winkel(point1, point2);

      this.Width = width;
      this.Height = height;
      this.Depht = depth;

      #region "Generic Property Setter"
      int lStatus;

      int Anz = 0;

      double dVal = 0;
      double[] daVal = null;
      int lVal = 0;
      int[] laVal = null;
      string sVal = null;
      string[] saVal = null;

      int Typ = (int)SOM.Helper.enums.ObjektTypen.Opening;
      IEZ_sp_Attribute cAttribut = new IEZ_sp_Attribute();



      foreach (System.Reflection.PropertyInfo p in _props)
      {
        if (p.PropertyType == typeof(double))
        {
          dVal = 0;
          cAttribut.GetDouble((int)Typ, ID, p.Name, out dVal, out lStatus);
          if (lStatus == 0)
          {
            this.GetType().GetProperty(p.Name).SetValue(this, dVal, null);
          }
        }
        else if (p.PropertyType == typeof(double[]))
        {
          daVal = null;
          Anz = 0;
          cAttribut.GetArraySize(Typ, ID, p.Name, out Anz, out lStatus);
          cAttribut.GetDoubleArray(Typ, ID, p.Name, 0, Anz, out daVal, out lStatus);
          if (lStatus == 0)
          {
            this.GetType().GetProperty(p.Name).SetValue(this, daVal, null);
          }
        }
        else if (p.PropertyType == typeof(int))
        {
          lVal = 0;
          cAttribut.GetInt(Typ, ID, p.Name, out lVal, out lStatus);
          if (lStatus == 0)
          {
            this.GetType().GetProperty(p.Name).SetValue(this, lVal, null);
          }
        }
        else if (p.PropertyType == typeof(int[]))
        {
          laVal = null;
          Anz = 0;
          cAttribut.GetArraySize(Typ, ID, p.Name, out Anz, out lStatus);
          cAttribut.GetIntArray(Typ, ID, p.Name, 0, Anz, out laVal, out lStatus);
          if (lStatus == 0)
          {
            this.GetType().GetProperty(p.Name).SetValue(this, laVal, null);
          }
        }
        else if (p.PropertyType == typeof(string))
        {
          sVal = null;
          cAttribut.GetChar(Typ, ID, p.Name, out sVal, out lStatus);
          if (lStatus == 0)
          {
            this.GetType().GetProperty(p.Name).SetValue(this, sVal, null);
          }
        }
        else if (p.PropertyType == typeof(string[]))
        {
          saVal = null;
          Anz = 0;
          cAttribut.GetArraySize(Typ, ID, p.Name, out Anz, out lStatus);
          cAttribut.GetCharArray(Typ, ID, p.Name, 0, Anz, out saVal, out lStatus);
          if (lStatus == 0)
          {
            this.GetType().GetProperty(p.Name).SetValue(this, saVal, null);
          }
        }
      }
      #endregion


      this.SetPropertiesReadOnly(true);

    }

  }
}
